package com.coocaa.ops.admin.v1.cornericon;

import com.alibaba.fastjson.JSONArray;
import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.annotation.Config;
import com.coocaa.ops.admin.base.controller.MgrBaseController;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.cornericon.model.GlobalCornerIconModel;
import com.coocaa.ops.admin.v1.cornericon.service.ICornerIconQueryService;
import com.coocaa.ops.admin.v1.cornericon.service.ICornerIconUpdateService;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import com.coocaa.ops.common.core.biz.cornericon.entity.GlobalCornerIconEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2018/6/19.
 * @description: 角标 -前端控制器
 */
@Slf4j
@RestController
/**
 * 菜单elid
 */
@Config(menuToken = "cornerIcon")
@RequestMapping("/v1/cornerIcon")
public class CornerIconController extends MgrBaseController {
    @Autowired
    private ICornerIconQueryService cornerIconQueryService;
    @Autowired
    private ICornerIconUpdateService cornerIconUpdateService;
    private static CommonLanguageProperties commonLanguageProperties;

    /**
     * 保存
     *
     * @param request
     * @param jsonStr
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "/save", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public Object save(HttpServletRequest request, String jsonStr) {
        List<GlobalCornerIconEntity> iconList = JSONArray.parseArray(jsonStr, GlobalCornerIconEntity.class);
        cornerIconUpdateService.saveBatch(iconList, WebUtil.getCurrentUser(request));
        return R.ok().msg(commonLanguageProperties.getSaveSuccess());
    }

    /**
     * 获取详情信息
     *
     * @param id
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getDetailInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public Object getDetailInfo(Integer id) {
        InputParamsValidator.validIdValue(id);
        GlobalCornerIconEntity entity = cornerIconQueryService.getDetailInfo(id);
        Map result = R.ok(entity);
        return render2Json(result);
    }


    /**
     * 分页数据
     *
     * @param request
     * @param model
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/pageList", method = {RequestMethod.GET, RequestMethod.POST})
    public Object pageList(HttpServletRequest request, GlobalCornerIconModel model) {
        Map<String, Object> data = cornerIconQueryService.getPageResult(model);
        return render2Json(data);
    }

    /**
     * 删除记录
     *
     * @param
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.DELETE)
    @RequestMapping(value = "/remove", method = {RequestMethod.GET, RequestMethod.POST})
    public Object remove(Integer[] id, HttpServletRequest request) {
        return cornerIconUpdateService.deleteBatch(id, WebUtil.getCurrentUser(request));
    }

    /**
     * 批量审核
     *
     * @param request
     * @param idStr
     * @param auditFlag
     * @param auditDesc
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BATCHAUDIT)
    @RequestMapping(value = "/batchAudit", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public Object batchAudit(HttpServletRequest request, String idStr, Integer auditFlag, String auditDesc) {
        SysUserEntity user = WebUtil.getCurrentUser(request);
        cornerIconUpdateService.batchAudit(idStr, user, auditFlag, auditDesc);
        return R.ok().msg(commonLanguageProperties.getAuditSuccess());

    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        CornerIconController.commonLanguageProperties = commonLanguageProperties;
    }

    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getIconAttributes", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String getIconAttributes(HttpServletRequest request) {
        Map result = cornerIconQueryService.getIconAttributes();
        return render2Json(result);

    }

    /**
     * 获取菜单运行权限列表
     *
     * @param request
     * @param
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getMenuInfo", method = {RequestMethod.GET}, produces = "text/html;charset=UTF-8")
    public Object getMenuInfo(HttpServletRequest request) {
        Map result = getMenuInfo(this.getClass(), request);
        return render2Json(result);
    }

}
